package com.zpself.module.算法练习.String;

class 力扣_8_字符串转换整数 {
    public static void main(String[] args) {

        System.out.println(-42 ==myAtoi("    -42"));
        System.out.println(4193==myAtoi("4193 with words"));
        System.out.println(0==myAtoi("words and 987"));
        System.out.println(-2147483648==myAtoi("-91283472332"));
        System.out.println(0==myAtoi("+-12"));
        System.out.println(0==myAtoi("00000-42a1234"));
        System.out.println(2147483647==myAtoi("21474836460"));
        System.out.println(12345678==myAtoi("  0000000000012345678"));
        System.out.println(0==myAtoi("   +0 123"));
        System.out.println(2147483647==myAtoi("9223372036854775808"));
    }

    private static int myAtoi(String s) {
        long result = 0L;
        int minus = 1;
        boolean start =false;
        for (int i = 0; i < s.length(); i++) {
            char c= s.charAt(i);
            if (!start){
                if(c==' ') continue;
                if(c=='-') { start=true;minus = -1;continue;}
                if(c=='+' ||c=='0') { start=true;continue;}
            }
            if(48<=c && c<=57) {
                start=true;
                result = result*10 + Integer.valueOf(c+"");
                result = result>= 2147483648L ? 2147483648L :result;
            }
            else break;
        }
        result= result*minus;
        if(result>Integer.MAX_VALUE) return Integer.MAX_VALUE;
        return (int) result;
    }
}